package com.johanChan.utils;

import java.util.ArrayList;
import java.util.List;

public class CommonUtils {

    public static String buildTree(List<TreeBuilder.Node> nodeList){
        String jsonStr = "";
        if (null != nodeList && nodeList.size() > 0){
            try{
//                System.out.println(System.currentTimeMillis() + jsonStr);
                jsonStr = new TreeBuilder().buildTree(nodeList);
//                System.out.println(System.currentTimeMillis() + ":" + jsonStr);
            }catch (Exception e){
//                System.out.println(e);
            }
        }
        return jsonStr;
    }

    //根据父节点获取全部子节点
    public static List<TreeBuilder.Node> getChildren(Integer pid,List<TreeBuilder.Node> nodeList){
        List<TreeBuilder.Node> resultList = new ArrayList<>();
        if (null != nodeList && nodeList.size() > 0){
            try{
                for (TreeBuilder.Node layer : nodeList) {
                    if(pid == layer.getParentId()){
                        //添加子级节点
                        resultList.add(layer);
                        //递归获取深层节点
                        resultList.addAll(getChildren(layer.getId(),nodeList));
                    }
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }
        return resultList;
    }

    public static List<TreeBuilder.Node> buildNode(){
        List<TreeBuilder.Node> nodeList = new ArrayList<>();
        TreeBuilder.Node node = new TreeBuilder.Node(0,-1,"地球","WORD",1);
        TreeBuilder.Node node1 = new TreeBuilder.Node(1,0,"中国","CHN",1);

        TreeBuilder.Node node2 = new TreeBuilder.Node(2,1,"华北区域","A",2);
        TreeBuilder.Node node3 = new TreeBuilder.Node(3,1,"华南区域","B",2);
        TreeBuilder.Node node4 = new TreeBuilder.Node(4,1,"华东区域","C",2);
        TreeBuilder.Node node5 = new TreeBuilder.Node(5,1,"华西区域","D",2);
        TreeBuilder.Node node6 = new TreeBuilder.Node(6,1,"华中区域","E",2);

        TreeBuilder.Node node7 = new TreeBuilder.Node(7,6,"河南","豫",3);
        TreeBuilder.Node node8 = new TreeBuilder.Node(8,6,"湖北","鄂",3);
        TreeBuilder.Node node9 = new TreeBuilder.Node(9,6,"湖南","湘",3);
        TreeBuilder.Node node10 = new TreeBuilder.Node(10,6,"江西","赣",3);

        TreeBuilder.Node node11 = new TreeBuilder.Node(11,7,"郑州","豫A",4);
        TreeBuilder.Node node12 = new TreeBuilder.Node(12,7,"开封","豫B",4);
        TreeBuilder.Node node13 = new TreeBuilder.Node(13,7,"洛阳","豫C",4);
        TreeBuilder.Node node14 = new TreeBuilder.Node(14,7,"南阳","豫R",4);

        TreeBuilder.Node node15 = new TreeBuilder.Node(15,11,"金水区","豫A-1",5);

        nodeList.add(node);
        nodeList.add(node1);nodeList.add(node2);nodeList.add(node3);
        nodeList.add(node4);nodeList.add(node5);nodeList.add(node6);
        nodeList.add(node7);nodeList.add(node8);nodeList.add(node9);
        nodeList.add(node10);nodeList.add(node11);nodeList.add(node12);
        nodeList.add(node13);nodeList.add(node14);nodeList.add(node15);

        return nodeList;
    }

    public static List<Tree<TreeBuilder.Node>> buildTree(){
        List<Tree<TreeBuilder.Node>> treeList = new ArrayList<>();
        Tree<TreeBuilder.Node> tree = new Tree<TreeBuilder.Node>(0,0,"地球",1);
        Tree tree1 = new Tree(1,0,"中国",1);

        Tree tree2 = new Tree(2,1,"华北区域",2);
        Tree tree3 = new Tree(3,1,"华南区域",2);
        Tree tree4 = new Tree(4,1,"华东区域",2);
        Tree tree5 = new Tree(5,1,"华西区域",2);
        Tree tree6 = new Tree(6,1,"华中区域",2);

        Tree tree7 = new Tree(7,6,"河南",3);
        Tree tree8 = new Tree(8,6,"湖北",3);
        Tree tree9 = new Tree(9,6,"湖南",3);
        Tree tree10 = new Tree(10,6,"江西",3);

        Tree tree11 = new Tree(11,7,"郑州",4);
        Tree tree12 = new Tree(12,7,"开封",4);
        Tree tree13 = new Tree(13,7,"洛阳",4);
        Tree tree14 = new Tree(14,7,"南阳",4);

        Tree tree15 = new Tree(15,11,"金水区",5);

        treeList.add(tree);
        treeList.add(tree1);treeList.add(tree2);treeList.add(tree3);
        treeList.add(tree4);treeList.add(tree5);treeList.add(tree6);
        treeList.add(tree7);treeList.add(tree8);treeList.add(tree9);
        treeList.add(tree10);treeList.add(tree11);treeList.add(tree12);
        treeList.add(tree13);treeList.add(tree14);treeList.add(tree15);

        return treeList;
    }

}
